package com.kadron.common;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PasswordUtil implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2979449495158395342L;
	
	private static final Log log_ = LogFactory.getLog(PasswordUtil.class);
	
	public static String cript(String key) throws NullPointerException {
		if (KUtil.isEmpty(key)) {
			throw new NullPointerException("key je prazan");
		}
		byte[] defaultBytes = key.getBytes();
		String cripted = null;
		try{
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(defaultBytes);
			byte messageDigest[] = algorithm.digest();
		            
			StringBuffer hexString = new StringBuffer();
			for (int i=0;i<messageDigest.length;i++) {
				hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
			}
			String foo = messageDigest.toString();
			if (log_.isDebugEnabled()) {
				log_.debug("key "+key+" md5 version is "+hexString.toString());
				log_.debug("MessageDigest: " +foo);				
			}
			cripted=hexString+"";
		} catch(NoSuchAlgorithmException nsae) {
			nsae.printStackTrace();
		}
		return cripted;
	}
	
}
